package com.aiiju.core.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import com.aiiju.core.service.tool.IoService;
import com.aiiju.core.service.tool.ParseService;
import com.aiiju.core.service.tool.ReadService;
import com.aiiju.core.util.BytesHexStrTranslate;
import com.aiiju.core.util.DeleteFileUtil;
import com.aiiju.core.util.DocToHtml;
import com.aiiju.core.util.FileCodeUtils;
import com.aiiju.core.util.HtmlToString;
import com.aiiju.core.util.OpenOffice2PDF;
import com.aiiju.core.util.UrlUtils;
import com.sun.javafx.iio.ios.IosDescriptor;
import com.tongdainfo.commom.util.FileToString;
import com.tongdainfo.common.parser.IParser;
import com.tongdainfo.common.parser.PdfParser;
import com.tongdainfo.common.parser.Word98_2003Parser;

public class Demo {

	@Test
	public void readWord() throws Exception {
		// 根据实际情况进行修改
		String filePath = "C:\\Users\\aiju\\Desktop\\bug\\a.pdf";
		String fileContent = FileToString.getFileContent(filePath);
		System.err.println(fileContent);
	}
	
	@Test
	public void rea2dWord() throws Exception {
		// 根据实际情况进行修改
		File file =  new File("C:\\Users\\aiju\\Desktop\\bug\\a.doc");
		IParser  parse = new Word98_2003Parser();
		String result = parse.readText(file, "GB2312");
		
		System.out.println(result);
	}
	
	@Test
	public void readWordd() throws Exception {
		String filename = "C:\\Users\\aiju\\Desktop\\bug\\a.doc";
		FileToString fileToString = new FileToString();
		InputStream fileInputStream = new FileInputStream(filename);
		
		String fileContent = fileToString.getFileContent(filename, fileInputStream);
		System.err.println(fileContent);
	}
	
	@Test
	public void readTxt() throws Exception {
		
		File file = new File("‪");
		FileInputStream fileInputStream = new FileInputStream("D:\\1.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GB2312");
        
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
         
        StringBuffer sb = new StringBuffer();
        String text = null;
        while((text = bufferedReader.readLine()) != null){
            sb.append(text);
        }
        System.err.println(sb.toString());
//        return sb.toString();
	}
	
	@Test
	public void readTxt2() throws Exception {
		File file = new File("D:\\resource\\zhilian.doc");
	      URL url = file.toURL();
	    
	    
	    // 解析成功, 开始进行数据读取
	    String html = IOUtils.toString(url.openStream(), "UTF-8");
//	    System.err.println(html);
	    Document doc  = Jsoup.parse(html);
	    
	    String currentKeyWord = null;
	    Elements elems = doc.select("table td");
	    for(Element elem : elems) {
	      String text = elem.text();
	      System.out.println(text);
//	        System.out.println(currentKeyWord + text);
	        currentKeyWord = null;
	    }
	}
	@Test
	public void readTxdt2() throws Exception {
		String path = "D:\\resource\\zhilian.doc";
		String docToHtml = DocToHtml.docToHtml(path);
		String htmlToString = HtmlToString.htmlToString(docToHtml);
		System.err.println(htmlToString);
	}
	
	@Test
	public void readTxdddt2() throws Exception {
		/*File file = new File("D:\\resource\\zhilian.doc");
	    URL url = file.toURL();
	    String  html = IOUtils.toString(url.openStream(), "UTF-8");
		System.out.println(html);*/
		
		InputStream input = new FileInputStream(new File("D:\\resource\\zhilian.doc"));
		String string = IOUtils.toString(input, "UTF-8");
		System.out.println(string);
		
		String htmlToString = HtmlToString.htmlToString(string);
		System.err.println(htmlToString);
	}
	
	@Test
	public void readTxdfdedddt2() throws Exception {
//		String filename = "C:\\Users\\aiju\\Desktop\\1.html";
		InputStream input = UrlUtils.urlToIO("http://192.168.4.195:8089/1.doc");
//		InputStream input = new FileInputStream(new File(filename));
		String codeString = FileCodeUtils.codeString(input);
		System.out.println(codeString);
	}
	
	@Test
	public void readTs() throws Exception {
		/*String inputFilePath = "D:\\resource\\liepin2.doc";
    	String outputFilePath = "D:\\resource\\liepin2.pdf";*/

        OpenOffice2PDF office2pdf = new OpenOffice2PDF();
        /*office2pdf.openOffice2Pdf( "D:\\resource\\liepin2.doc",
                "D:\\resource\\convert\\" + UUID.randomUUID().toString() + "."
                        + PDF_POSTFIX);*/
        office2pdf.openOffice2Pdf( "D:\\resource\\job51.doc",
        		"D:\\resource\\convert\\"+UUID.randomUUID().toString()+".pdf");
        System.err.println("Over");
	}

	@Test
	public void readTsss() throws Exception {
		String cachFilePath = System.getProperty("java.io.tmpdir");
		System.out.println(cachFilePath);
		
		InputStream fileInputStream = new FileInputStream("D:\\resource\\job51.doc");
		File file = new File("D:\\resource\\job512.doc");
		FileUtils.copyInputStreamToFile(fileInputStream, file);
		System.out.println("file");
	}
	
	
	
	@Test
	public void sssd() throws Exception {

        String text = "auser1 home1b\n" +
                "auser2 home2b\n" +
                "auser3 home3b";
   
        Matcher m = Pattern.compile("(?m)^.*$").matcher(text);

        StringBuilder sb = new StringBuilder();
        while (m.find()) {
//            System.out.println("line = " + m.group());
            sb.append(m.group()).append("\r\n");
        }
        System.out.println(sb.toString());
    }
	
	@Test
	public void sd() throws Exception {
		String content = ReadService.readFile("D:\\resource\\e.txt");
		String newContent = ReadService.toNewString(content);
		String parseTxt2 = new ParseService().parseTxt(content);
		System.out.println("\r\n");
		 String parseTxt = new ParseService().parseTxt(newContent);
		
	}
	
	@Test
	public void dscfeswfsd() throws Exception {
		/*InputStream input = new FileInputStream(new File("D:\\resource\\lagou.pdf"));
		IParser parse = new PdfParser();
		String codeString = FileCodeUtils.codeString(input);
		
//		String result = parse.readInput(input,"GB2312");
//		String result = parse.readInput(input,"UTF-8");
		String result = parse.readInput(input,codeString);
		System.out.println(result);*/
		
		String fileName = "C:\\Users\\aiju\\AppData\\Local\\Temp\\a94e30797fc24e30a8d9712d3f6586fa.pdf";
		DeleteFileUtil.deleteFile(fileName);
		
	}
	
	@Test
	public void dscfesfsd() throws Exception {
		String path = "D:\\resource\\e.txt";
		InputStream input = new FileInputStream(new File(path));
		
		String orgFile = IoService.getFile(input, "txt");
		
		input.close();
		DeleteFileUtil.deleteFile(orgFile);
		
		
	}



}
